<?php
class gmer_user {
	var $id=false;
	var $user_name=false;
	var $gm_name=false;
	var $ip=false;
	var $gm_level=false; //gm层级
	var $correct_odds=false; //正确率
	var $last_login=false;
	var $num_working=false;
	var $num_finished=false;
	var $num_late=false;	
	var $session=false;	
	
	var $stat_reply=false;
	var $stat_not_reply=false;
	var $stat_qa=false;
	var $stat_all=false;
	function __construct() {
		$ip=get_ip();
		if($ip != '220.248.115.130' and !is_localhost()) die('需要在公司内部，才可以使用GM后台!');
	}
	function user_init(){
		$nick_name=$this->nick_name;
		$sql="select * from gm_user where gm_name='$nick_name'";
		$res=run_sql($sql,__LINE__);
		if(!$row=mysql_fetch_row($res)) {
			//未找到该用户，初始化
			js_alert('该用户未开通权限',false);
		}
		$this->session=true;
		$this->get_user_info();
		return;
	}
	//从数据库获取用户信息
	function get_user_info(){
		$nick_name=$this->nick_name;
		$sql="select * from gm_user where gm_name='$nick_name'";
		$res=run_sql($sql,__LINE__);
		if(!$row=mysql_fetch_array($res)) {
			die('error: no user');
		}else {
			$this->ip=$row['ip'];
			$this->last_login=$row['last_login'];
			$this->gm_name=$row['gm_name'];
			$this->gm_level=$row['gm_level'];
			$this->correct_odds=$row['correct_odds']*100;
			$this->id=$row['id'];
			$this->num_working=$row['num_working'];
			$this->num_finished=$row['num_finished'];
			if(!isset($_COOKIE['gm_login_flag'])) {
				$this->update_login();
			}
			setcookie('gm_login_flag','yes',0,'/');
		}
		$this->get_ques_count();
		$this->get_stat();
		
	}
	//更新登录
	function update_login() {
		$ip=get_ip();
		$last_login=get_date_time();
		$id=$this->id;
		$sql="update gm_user set ip='$ip' , last_login='$last_login' where id=$id";
		run_sql($sql,__LINE__);
	}
	//设置用户数据库信息
	function set_user_info() {
		
	}
	function get_ques_count() {
		$gm_id=$this->id;
		$sql="select count(*) from question_info where gm_id=$gm_id";
		$res=sql_get_one($sql,__LINE__);
		//单据总数
		$all_count=$res['count(*)'];
		$sql="select count(*) from question_info where gm_id=$gm_id and solution_flag=1";
		$res=sql_get_one($sql,__LINE__);
		//处理完的问题
		$finished_count=$res['count(*)'];
		$sql="select count(*) from question_info where gm_id=$gm_id and back_check=1";
		$res=sql_get_one($sql,__LINE__);
		//质检过的问题总数
		$check_count=$res['count(*)'];
		$sql="select count(*) from question_info where gm_id=$gm_id and back_check=1 and check_yes_or_no=1";
		$res=sql_get_one($sql,__LINE__);
		//质检正确的问题
		$check_count_yes=$res['count(*)'];
		$sql="select count(*) from question_info where gm_id=0";
		$res=sql_get_one($sql,__LINE__);
		//为处理的问题总数
		$new_count=$res['count(*)'];
		//超时单据
		$sql="select count(*) from question_info where gm_id=$gm_id and back_overtime=1";
		$res=sql_get_one($sql,__LINE__);
		$this->num_late=$res['count(*)'];
		
		$this->count_unfinished=$all_count-$finished_count;
		$this->finished_count=$finished_count;
		if($check_count==0) $this->correct_odds='100%';
		else {
			$tmp=$check_count_yes/$check_count * 100 ;
			$this->correct_odds=$tmp.'%';
		}
		$this->new_count=$new_count;
 	}
 	function get_stat(){
 		$sql="select count(*) from question_info";
 		$res=sql_get_one($sql,__LINE__);
 		$this->stat_all=$res['count(*)'];
 		$sql="select count(*) from question_info where solution_flag=1";
 		$res=sql_get_one($sql,__LINE__);
 		$this->stat_reply=$res['count(*)'];
 		$this->stat_not_reply=$this->stat_all-$this->stat_reply;
 		$sql="select count(*) from question_info where back_check=1";
 		$res=sql_get_one($sql,__LINE__);
 		$this->stat_qa=$res['count(*)']; 		
 	}
}
?>